package com.tlkg.qa.redistest;

import java.util.Date;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import lombok.extern.log4j.Log4j;

@Log4j
public class StartTest {
	public static ExecutorService threadPool;
	// public static ThreadPoolExecutor s = new ThreadPoolExecutor(corePoolSize,
	// maximumPoolSize, keepAliveTime, unit, workQueue);
	// public static ThreadLocal tl = new ThreadLocal<>();

	public static void main(String[] args) {

		int client = 50;
		int num = 100000;
		int test = 1;

		for (String string : args) {
			if (string.startsWith("-J")) {
				String[] arg = string.substring(2).split("=");
				switch (arg[0]) {
				case "c":
					client = Integer.parseInt(arg[1]);
					break;
				case "n":
					num = Integer.parseInt(arg[1]);
					break;
				case "t":
					if (arg[1].equals("set")) {
						test = 1;
					} else if (arg[1].equals("get")) {
						test = 2;
					} else {
						System.err.println("参数有误！");
						System.exit(0);
					}
					break;
				default:
					break;
				}
			}
		}

		threadPool = Executors.newFixedThreadPool(client);

		if (test == 1) {
			log.info("===================SET==================");
		} else if (test == 2) {
			log.info("===================GET==================");
		}
		log.info("test start! " + new Date());
		log.info("summary: " + num + " request ," + client + " client");

		long start = System.nanoTime();

		for (int i = 0; i < num; i++) {
			TestThead testThead = new TestThead("test_" + i);
			testThead.setTest(test);
			threadPool.submit(testThead);
		}
		threadPool.shutdown();

		while (!threadPool.isTerminated()) {
		}
		
		long end = System.nanoTime();
		float duration = (end - start) / 1000000;
		log.info("测试执行完毕！ " + new Date());
		log.info("持续时间：" + duration + " ms");
		log.info("QPS：" + (num / duration) * 1000);
		if(test == 1){
			log.info("Set未命中次数" + Count.setmisscount.get());
		}else if(test==2){
			log.info("Get未命中次数" + Count.getmisscount.get());
		}
	}
}